/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package model.DAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import model.entities.CheckOut;
import model.entities.Checkin;
import model.entities.Login;
import model.entities.Reservation;
import model.entities.Room;

/**
 *
 * @author Hieu.Nguyen
 */
public class CheckOutDAO {
    private static CheckOutDAO checkOutDAO;

    public static CheckOutDAO getInstance() {
        if (checkOutDAO == null) {
            checkOutDAO = new CheckOutDAO();
        }
        return checkOutDAO;
    }
    
     public List<CheckOut> getCheckOut(boolean isActived) {
        List<CheckOut> listCheckOut = new ArrayList<CheckOut>();
        try {
            Connection conn = ConnectionFactory.getConnection();
            String sql = "select * from tblCheckOut";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            
            ResultSet rs = pstmt.executeQuery();
            RoomDAO roomD = new RoomDAO();
            LoginDAO liD = new LoginDAO();
            while (rs.next()) {
                CheckOut r = new CheckOut();
                r.setCheckoutID(rs.getInt("CheckoutID"));
                Reservation aa = ReservationDAO.getInstance().getReservationByID(rs.getInt("ReservationID"));                
                r.setReservation(aa);
                r.setCheckoutDate(rs.getDate("CheckoutDate"));
                r.setCheckoutTime(rs.getString("CheckoutTime"));
                
                Room aa1 = RoomDAO.getInstance().getRoomByID(rs.getInt("RoomID"));                
                r.setRoom(aa1);
                
                Login aa2 = LoginDAO.getInstance().getLoginByID(rs.getInt("UserID"));                
                r.setLogin(aa2);

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return listCheckOut;
    }
     
      public boolean createCheckout(CheckOut ckout) {
        boolean isCheck = false;
        try {
            Connection conn = ConnectionFactory.getConnection();
            String sql = "insert into tblCheckOut values (?,?,?,?,?,?)";
            PreparedStatement pstmt = conn.prepareStatement(sql);
           
            pstmt.setInt(1, ckout.getCheckoutID());
            pstmt.setInt(2, ckout.getReservation().getReservationID());
            pstmt.setDate(3, ckout.getCheckoutDate());
            pstmt.setString(4, ckout.getCheckoutTime());
            pstmt.setInt(5, ckout.getRoom().getRoomID());
            pstmt.setInt(6, ckout.getLogin().getUserID());

            pstmt.executeUpdate();
            isCheck = true;
        } catch (Exception e) {
            System.out.println(e.toString());
        }
        return isCheck;
     }
    
}
